package com.tea.controller;

import com.tea.service.FileStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileStorageService fileStorageService;

    @PostMapping("/upload")
    public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (file == null || file.isEmpty()) {
                result.put("success", false);
                result.put("message", "文件不能为空");
                return result;
            }
            
            String fileUrl = fileStorageService.uploadFile(file);
            log.info("文件上传成功，存储类型: {}, 文件URL: {}", fileStorageService.getStorageType(), fileUrl);
            
            result.put("success", true);
            result.put("message", "文件上传成功");
            result.put("data", fileUrl);
            result.put("storageType", fileStorageService.getStorageType());
            return result;
        } catch (IOException e) {
            log.error("文件上传失败: {}", file.getOriginalFilename(), e);
            result.put("success", false);
            result.put("message", "文件上传失败: " + e.getMessage());
            return result;
        }
    }
    
    @PostMapping("/upload/{directory}")
    public Map<String, Object> uploadFileToDirectory(
            @RequestParam("file") MultipartFile file,
            @PathVariable("directory") String directory) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (file == null || file.isEmpty()) {
                result.put("success", false);
                result.put("message", "文件不能为空");
                return result;
            }
            
            String fileUrl = fileStorageService.uploadFile(file, directory);
            log.info("文件上传成功到目录 {}, 存储类型: {}, 文件URL: {}", directory, fileStorageService.getStorageType(), fileUrl);
            
            result.put("success", true);
            result.put("message", "文件上传成功");
            result.put("data", fileUrl);
            result.put("storageType", fileStorageService.getStorageType());
            result.put("directory", directory);
            return result;
        } catch (IOException e) {
            log.error("文件上传到目录 {} 失败: {}", directory, file.getOriginalFilename(), e);
            result.put("success", false);
            result.put("message", "文件上传失败: " + e.getMessage());
            return result;
        }
    }
    
    @DeleteMapping("/delete")
    public Map<String, Object> deleteFile(@RequestParam("fileName") String fileName) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean deleted = fileStorageService.deleteFile(fileName);
            log.info("文件删除操作，存储类型: {}, 文件名: {}, 结果: {}", fileStorageService.getStorageType(), fileName, deleted);
            
            result.put("success", deleted);
            result.put("message", deleted ? "文件删除成功" : "文件删除失败或文件不存在");
            result.put("storageType", fileStorageService.getStorageType());
            return result;
        } catch (Exception e) {
            log.error("文件删除失败: {}", fileName, e);
            result.put("success", false);
            result.put("message", "文件删除失败: " + e.getMessage());
            return result;
        }
    }
    
    @DeleteMapping("/deleteByUrl")
    public Map<String, Object> deleteFileByUrl(@RequestParam("fileUrl") String fileUrl) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean deleted = fileStorageService.deleteFileByUrl(fileUrl);
            log.info("文件删除操作（URL），存储类型: {}, 文件URL: {}, 结果: {}", fileStorageService.getStorageType(), fileUrl, deleted);
            
            result.put("success", deleted);
            result.put("message", deleted ? "文件删除成功" : "文件删除失败或文件不存在");
            result.put("storageType", fileStorageService.getStorageType());
            return result;
        } catch (Exception e) {
            log.error("文件删除失败（URL）: {}", fileUrl, e);
            result.put("success", false);
            result.put("message", "文件删除失败: " + e.getMessage());
            return result;
        }
    }
    
    @GetMapping("/exists")
    public Map<String, Object> checkFileExists(@RequestParam("fileName") String fileName) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean exists = fileStorageService.doesFileExist(fileName);
            
            result.put("success", true);
            result.put("exists", exists);
            result.put("message", exists ? "文件存在" : "文件不存在");
            result.put("storageType", fileStorageService.getStorageType());
            return result;
        } catch (Exception e) {
            log.error("检查文件存在性失败: {}", fileName, e);
            result.put("success", false);
            result.put("message", "检查文件存在性失败: " + e.getMessage());
            return result;
        }
    }
    
    @GetMapping("/storageInfo")
    public Map<String, Object> getStorageInfo() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("storageType", fileStorageService.getStorageType());
        result.put("message", "当前使用 " + fileStorageService.getStorageType() + " 存储");
        return result;
    }
}
